<template>
    <div class="codeHistory-body">
        <!--<div class="search-div">
            &lt;!&ndash; 搜索框面板 &ndash;&gt;
            <div class="panel panel-success">
                <div class="panel-heading panel-title">搜索公司</div>
                <div class="panel-body form-body">
                    <div class="input-group form-search">
                        <input type="text" class="form-control"/>
                        <span class="input-group-addon btn btn-success font-style">搜索一下</span>
                    </div>

                    <ul class="company-list list-group">
                        <li class="list-group-item one-company" v-for="company in companySet">
                            {{company.epname}}
                        </li>
                    </ul>
                </div>
            </div>
        </div>-->


        <!-- 公司信息（隐藏面板） -->
        <div class="company-div" :style="{ display: showCompany }">
            <div class="panel panel-success">
                <div class="panel-heading panel-title"> 生成二维码</div>
                <div class="panel-body company-body">
                    <div class="row">

                        <!-- 公司和产品信息 -->
                        <div class="col-xs-8">
                            <!-- 公司已知 -->
                            <div class="row">
                                <div class="col-xs-12 company-info-1">
                                    <div class="img-area">
                                        <img class="img-thumbnail img-company" :src="companyInfo.logoImg"/>
                                    </div>
                                    <div class="text-area">
                                        <p class="first-p">
                                            <span class="text-info">公司名：</span>
                                            <a v-link="{ path: companyInfo.companyDetail }">{{companyInfo.epname}}</a>
                                            <!--<a v-link="{ path = companyInfo.companyDetail }">{{companyInfo.epname}}</a>-->
                                        </p>
                                        <p><span class="text-info">法人：</span>{{companyInfo.legalperson}}</p>
                                        <p><span class="text-info">地址：</span>{{companyInfo.address}}</p>
                                        <p class="last-p">
                                            <span class="text-info">经营范围：</span>
                                            {{companyInfo.runscope}}
                                        </p>
                                    </div>
                                    <div style="clear: both"></div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-xs-12 product-info-2">
                                    <div class="text-info">选择品种：</div>
                                    <div class="product-type">
                                        <label class="radio-type" v-for="item in productSet">
                                            <input type="radio" name="ptype" value="{{item.id}}"
                                                   v-model="CreateCodeText.typecode"/>{{item.name}}
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- 生成二维码参数 -->
                        <div class="col-xs-4">
                            <div class="form-group select-date">
                                <label for="1" class="text-info text-indate">生产地：</label>
                                <input type="text" class="form-control num-input" id="1"
                                       v-model="CreateCodeText.fromarea">
                            </div>
                            <div class="form-group select-date">
                                <label for="2" class="text-info text-indate">生产日期：</label>
                                <input type="text" class="form-control num-input" id="2"
                                       v-model="CreateCodeText.startdate">
                            </div>
                            <div class="form-group select-date">
                                <label for="3" class="text-info text-indate">保质期：</label>
                                <input type="text" class="form-control num-input" id="3"
                                       v-model="CreateCodeText.expdate">
                            </div>
                            <div class="form-group select-date">
                                <label for="4" class="text-info text-indate">二维码失效时间：</label>
                                <input type="text" class="form-control num-input" id="4"
                                       v-model="CreateCodeText.codeOvertime">
                            </div>
                            <div class="form-group">
                                <label for="5" class="text-info text-num">二维码数量：</label>
                                <input type="text" class="form-control num-input" id="5"
                                       v-model="CreateCodeText.codeNum">
                            </div>
                            <div>
                                <button type="button" class="btn btn-info my-btn-style" @click="CreateCode()">生成二维码
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 二维码生成历史记录 -->
        <div class="history-div">
            <div class="panel panel-success">
                <div class="panel-heading panel-title">二维码生成记录</div>
                <div class="panel-body">
                    <table class="table ">
                        <tr>
                            <th class="th-1">二维码批号</th>
                            <th>公司名称</th>
                            <th>品种名称</th>
                            <th>生成时间</th>
                            <th>数量</th>
                            <th>下载</th>
                        </tr>
                        <tr v-for="obj in historyList">
                            <td>{{obj.codePiNO}}</td>
                            <td>{{obj.epname}}</td>
                            <td>{{obj.typename}}</td>
                            <td>{{obj.codecreatetime}}</td>
                            <td>{{obj.codeNum}}</td>
                            <td><a href="{{obj.download}}">点击下载</a></td>
                        </tr>
                    </table>
                </div>
                <div class="footer">
                    <nextpage></nextpage>
                </div>
            </div>
        </div>
    </div>

</template>

<script>
    require('../assets/css/viewModels/codeHistory.less');
    module.exports = {
        data: function () {
            return {
                showCompany: 'none',
                companyInfo: {
                    logoImg: '',
                    epname: '',
                    address: '',
                    runscope: '',
                    legalperson: '',
                    companyDetail: '/index/companyInfo/'
                },
                productSet: [],
                CreateCodeText: {
                    regcode: '',
                    typecode: '',
                    fromarea: '',
                    startdate: '',
                    expdate: '',
                    codeOvertime: '',
                    codeNum: ''
                },
                historyList: []
            }
        },
        ready: function () {
            var _this = this;
            var company_id = this.$route.params.company_id;
            // 确定公司显示生成二维码页面
            if (company_id) {
                // 显示隐藏面板
                _this.showCompany = 'block';
                // 查找公司信息
                $.post('/Enterprise/FindById', {"id": company_id}).then(function (data) {
                    _this.companyInfo.logoImg = data.content.logo;
                    _this.CreateCodeText.regcode = data.content.regcode;
                    _this.companyInfo.epname = data.content.epname;
                    _this.companyInfo.address = data.content.address;
                    _this.companyInfo.legalperson = data.content.legalperson;
                    _this.companyInfo.runscope = data.content.runscope;
                    _this.companyInfo.companyDetail = _this.companyInfo.companyDetail + company_id;
                });
                // 查找产品列表
                $.post('/Product/findall').then(function (data) {
                    var count = data.content.count;
                    _this.selected_id = data.content.list[0].proID;
                    for (var i = 0; i < count; i++) {
                        var obj = {
                            id: data.content.list[i].proID,
                            name: data.content.list[i].proName
                        };
                        _this.productSet.push(obj);
                    }
                });
            }
            // 查看历史记录
            $.post('/QRcode/CreateHistory', { "regcode": company_id }).then(function (data) {
                var list = data.content.list;
                for (var i = 0; i < list.length; i++) {
                    var obj = {
                        codePiNO: list[i].codePiNO,
                        epname: list[i].epname,
                        typename: list[i].typename,
                        codecreatetime: list[i].codecreatetime,
                        codeNum: list[i].codeNum,
                        download: '/QRcode/DownloadCodeZipFile?codePiNO=' + list[i].codePiNO
                    };
                    _this.historyList.push( obj );
                }
            });
        },
        methods: {
            CreateCode: function () {
                var _this = this;
                var args = {
                    regcode: _this.CreateCodeText.regcode,
                    typecode: _this.CreateCodeText.typecode,
                    fromarea: _this.CreateCodeText.fromarea,
                    startdate: _this.CreateCodeText.startdate,
                    expdate: _this.CreateCodeText.expdate,
                    codeOvertime: _this.CreateCodeText.codeOvertime,
                    codeNum: _this.CreateCodeText.codeNum
                };
                $.post('/QRcode/QRcodeGenerate', args).then(function (data) {
                    if (data.status == 'success') {
                        alert('二维码生成成功！');
                    }
                });
            }
        },
        components: {
            'topnav': require('../components/topnav.vue'),
            'nextpage': require('../components/nextpage.vue')
        }
    }
</script>
